<!-- TMS订单管理 - 综合表单页面 -->
<template>
  <div class="page-container">
    <page_breadcrumb :getKeyMethod="init_table_model" @change="change_table_model" />

    <ex_super_table_page v-bind="option" ref="SuperTablePage" @selection-change="e => selection = e">
    </ex_super_table_page>


    <waybill_task_intercept ref="waybill_task_intercept" @submitSuccess="reload()" />
    <waybill_task_reservation ref="waybill_task_reservation" @submitSuccess="reload()" />
    <waybill_task_adjust_carrier_driver ref="waybill_task_adjust_carrier_driver" @submitSuccess="reload()" />
    <waybill_task_transport ref="waybill_task_transport" @submitSuccess="reload()" />

    <waybill_task_confirm_completion ref="waybill_task_confirm_completion" @submitSuccess="reload()" />

    <waybill_task_pick_up_upload ref="waybill_task_pick_up_upload" @submitSuccess="reload()" />
    <waybill_task_sign_upload ref="waybill_task_sign_upload" @submitSuccess="reload()" />
    <waybill_task_qty_modify ref="waybill_task_qty_modify" @submitSuccess="reload()" />
    <report_anomaly ref="report_anomaly" :orderTypeList="['TRANSPORT']" :formDisabled="['orderType', 'customerId']"
      :formHidden="['orderNo','customerId']" :extraModel="[
        {
          key: 'orderNo',
          label: 'label_order_code',
          render: (h, { data }) => {
            if(this.orderNoData.length>0){
              this.$set(data, 'orderNo', this.orderNoData[0].trackNumber);
              this.$set(data, 'anomalyBoxQty', this.orderNoData[0].boxQty);
              this.$set(data, 'anomalySkuQty', this.orderNoData[0].skuQty);
              this.$set(data, 'customerId', this.orderNoData[0].customerId);
              this.$set(data, 'customerCode', this.orderNoData[0].customerCode);
              this.$set(data, 'customerName', this.orderNoData[0].customerName);
            }

            return h('ex_select_auto', {
              class: 'form_input_default',
              props: {
                value: data.orderNo,
                filterable: true,
                items: this.orderNoData,
                option: { label: 'trackNumber', value: 'trackNumber' },
              },
              on: {
                changeItem: (e) => {
                  this.$set(data, 'orderNo', e.trackNumber);
                  this.$set(data, 'anomalyBoxQty', e.boxQty);
                  this.$set(data, 'anomalySkuQty', e.skuQty);
                  this.$set(data, 'customerId', e.customerId);
                  this.$set(data, 'customerCode', e.customerCode);
                  this.$set(data, 'customerName', e.customerName);
                },
              },
            });
          },
        }
      ]" @afterSubmitSuccess="reload()" />
    <!-- 到仓预约 -->
    <appointmentReport ref="appointmentReportRer"></appointmentReport>

    <!-- 任务下发 -->
    <waybill_task_Issued ref="waybillTaskIssuedRef"  @submitSuccess="reload()" ></waybill_task_Issued>
  </div>
</template>
<script>
import superTablePageFunction from '@/mixin/super-table-page-function';

import page_table_model_manage from '@/mixin/page_table_model_manage';
import router_params from '@/mixin/router_params';
import waybill_task_reservation from '@/component/tms/waybill_task_reservation'
import waybill_task_intercept from '@/component/tms/waybill_task_intercept'
import waybill_task_adjust_carrier_driver from '@/component/tms/waybill_task_adjust_carrier_driver'
import waybill_task_transport from '@/component/tms/waybill_task_transport'

import waybill_task_confirm_completion from '@/component/tms/waybill_task_confirm_completion'
import waybill_task_pick_up_upload from '@/component/tms/waybill_task_pick_up_upload'
import waybill_task_sign_upload from '@/component/tms/waybill_task_sign_upload'
import waybill_task_qty_modify from '@/component/tms/waybill_task_qty_modify'

import appointmentReport from '@/component/tms/warehouse/appointment_report';

import report_anomaly from '@/component/dialog/report_anomaly.vue'



// 任务下发
import waybill_task_Issued from '@/component/tms/waybill_task_Issued'

import emitOnFunction from "@/mixin/emit-on-function";
let emitKey = "waybill_task_manage>confirm";
export default {
  title: "运输任务单",
  mixins: [superTablePageFunction, page_table_model_manage, router_params, emitOnFunction(emitKey),],
  components: {
    waybill_task_reservation, waybill_task_intercept, waybill_task_adjust_carrier_driver,
    waybill_task_transport, waybill_task_confirm_completion, waybill_task_pick_up_upload, waybill_task_sign_upload,
    waybill_task_qty_modify, appointmentReport, report_anomaly,waybill_task_Issued
  },
  data() {
    return {
      waybillTaskType: "",
      selection: [],
      option: {
        //搜索框内容
        search: {
          //搜索框内容，key为搜索字段，label为标签提示
          model: [

            { key: 'waybillTaskCode', label: 'label_tms_task_number' },
            { key: 'waybillCode', label: 'label_tms_waybill_waybill_number' },
            {
              key: 'status', label: 'label_tms_waybill_waybill_main_status', type: 'select', enum: 'TmsTaskStatusEnum',
            },
            { key: 'transportModel', label: 'label_tms_waybill_transport_mode', type: 'select', enum: "LogisticsProductsTypeEnum", },
            { key: 'transportSection', label: 'label_tms_line_transport_section', type: 'select', enum: "TransportSectionEnum", },

            // { key: 'taskType', label: 'label_order_type', type: 'select', enum: 'TrackTaskTypeEnum' },
            { key: 'originAddress', label: 'label_tms_start_address', fold: true },
            { key: 'destinationAddress', label: 'label_tms_waybill_end_addr', fold: true },
            { key: 'isReceipt', label: 'label_tms_waybill_waybill_upload', type: 'select', enum: "YesOrNoEnum", fold: true, },
            { key: 'isSchedule', label: 'label_tms_waybill_waybill_schedule', type: 'select', enum: "YesOrNoEnum", fold: true, },
            { key: 'orderCode', label: 'label_order_code' , fold: true, },
            { key: 'trackNumber', label: '物流订单编号' , fold: true, },
            { key: 'carrierName', label: 'label_srm_supplier_name', fold: true, },
            //订单编号,物流订单编号,服务商名称
          ],
        },
        actionExtra: [
          {
            label: "物流轨迹导入",
            if: () => this.waybillTaskType == "EXECUTE_TASK",
            click: () => {
              this.$router.push('/page/tms/track/track_import')
            }
          },

          { label: "预约申报", attrs: { type: "primary" }, click: ()=>this.appointmentReportFn() },
          { label: 'label_tms_waybill_task_delivery', attrs: { type: "primary" }, click: this.clickTaskIssued },
          { label: '预约下发', attrs: { type: "primary" }, click: this.clickReservationIssued },
          { label: 'label_tms_waybill_cancel_delivery', attrs: { type: "danger" }, click: this.clickRecallLssued },
          { label: 'label_tms_waybill_task_intercept', attrs: { type: "danger" }, click: this.clickTaskIntercept },
          { label: 'label_tms_waybill_cancel_scheduling', attrs: { type: "danger" }, click: this.clickRecallScheduling },
          { label: 'label_tms_waybill_batch_feedback', attrs: { type: "danger", disabled: true, }, click: this.$noFunction },
          { label: 'label_export', attrs: { type: "primary" }, click: this.exportExcel },

        ],

        table: {
          url: "/tms-api/waybill/TmsTaskInfoQueryRpc/selectByPage.json",
          urlParams: (params) => {
            return Object.assign({
              waybillTaskType: this.waybillTaskType
            }, params)
          },
          model: this.init_table_model(),
        }
      },
      orderNoData: [],
    };
  },
  methods: {
    init_data_default(into_params) {
      if (into_params.waybillTaskType) {
        this.waybillTaskType = into_params.waybillTaskType

        this.$emit("tagModifyLabel", "运输任务",)
      }
      if (into_params.waybillCode) {
        this.$refs.SuperTablePage.setSearchData({
          waybillCode: into_params.waybillCode
        })
      }
    },
    option_table_model() {
      return [
        { type: "selection", clickToggle: true },
        { key: 'waybillTaskCode', label: 'label_tms_task_number', 'show-overflow-tooltip': true, },
        { key: 'waybillCodes', label: 'label_tms_waybill_waybill_number', 'show-overflow-tooltip': true, },
        {
          key: 'status', label: 'label_tms_waybill_waybill_main_status', 'min-width': 120, 'show-overflow-tooltip': true,
          formatter: (r, c, v, i) => this.$enumShow(v, "TmsTaskStatusEnum")
        },
        { key: 'carrierName', label: 'label_tms_waybill_carrier_name', 'min-width': 120, 'show-overflow-tooltip': true, },
        { key: 'startAddress', label: 'label_tms_receipt_commence_address', 'min-width': 100, 'show-overflow-tooltip': true, },
        { key: 'endAddress', label: 'label_tms_waybill_destination', 'min-width': 100, 'show-overflow-tooltip': true, },
        { key: 'totalQuantity', label: 'label_tms_waybill_total_unit_quantity', 'width': 120, },
        { key: 'totalBox', label: 'label_tms_waybill_total_cartons', 'width': 120, },
        { key: 'totalWeight', label: 'label_tms_waybill_total_weight', 'width': 120, },
        { key: 'totalVolume', label: 'label_tms_waybill_total_volume', 'width': 120, },
        { key: 'isSchedule', label: 'label_tms_waybill_waybill_schedule', 'width': 120, formatter: (r, c, v) => this.$enumShow(v, "YesOrNoEnum") },
        { key: 'esArrivalTime', label: 'label_expected_arrival_date', 'width': 130, formatter: (r, c, v, i) => this.$dateFormat(v), },
        { key: 'gmtCreate', label: 'label_create_time', 'width': 130, formatter: (r, c, v, i) => this.$dateFormat(v), },
        {
          type: 'operate', width: 210, operate: [

            {
              type: 'more',
              items: [
                {
                  label: "label_tms_waybill_check_track",
                  disabled: (row) => !row.buttonList.includes(3),
                  click: (row) => {
                    this.$router.push({ path: '/page/tms/track/track_list', query: { trackNumber: row.trackNumbers.replace(/,/g," "), } })
                  }
                },
                {
                  label: "label_tms_waybill_task_view",
                  disabled: (row) => !row.buttonList.includes(950),
                  click: (row) => {
                    this.$router.push({ path: '/page/tms/order/waybill_task_detail', query: { id: row.id, } })
                  }
                },
                {
                  label: "label_tms_waybill_adjust_carriers_drivers",
                  disabled: (row) => !row.buttonList.includes(770),
                  click: (row) => {
                    this.$refs.waybill_task_adjust_carrier_driver.show(row)
                  }
                },
                /*{
                  label: "label_tms_waybill_quantity_modification",
                  disabled: (row) => !row.buttonList.includes(800),
                  if: () => this.waybillTaskType == "WAIT_EXECUTE_TASK",
                  click: (row) => {
                    this.$refs.waybill_task_qty_modify.show(row)
                  }
                },*/

                {
                  label: "label_tms_waybill_transport_dispatch",
                  disabled: (row) => !row.buttonList.includes(10),
                  click: (row) => {
                    this.$refs.waybill_task_transport.show(row)
                  }
                },
                {
                  label: "label_tms_waybill_pick_up_upload",
                  disabled: (row) => !row.buttonList.includes(830),
                  click: (row) => {
                    this.$refs.waybill_task_pick_up_upload.show(row)
                  }
                },
                {
                  label: "label_tms_waybill_sign_upload",
                  disabled: (row) => !row.buttonList.includes(840),
                  click: (row) => {
                    this.$refs.waybill_task_sign_upload.show(row)
                  }
                },
                {
                  label: "label_tms_waybill_confirm_complete",
                  disabled: (row) => !row.buttonList.includes(11),
                  click: (row) => {
                    this.$refs.waybill_task_confirm_completion.show(row)
                  }
                },
                {
                  label: "label_tms_waybill_take_orders",
                  disabled: (row) => !row.buttonList.includes(850),
                  click: (row) => {
                    this.submitTaskReceive(row)
                  }
                },
                {
                  label: "label_tms_waybill_depart",
                  disabled: (row) => !row.buttonList.includes(860),
                  click: (row) => {
                    this.submitDispatch(row)
                  }
                },
                /*{
                  label: "label_tms_waybill_arrive_pick_point",
                  disabled: (row) => !row.buttonList.includes(870),
                  if: () => this.waybillTaskType == "WAIT_EXECUTE_TASK",
                  click: (row) => {
                    this.submitArrivePoint(row)
                  }
                },*/
                // {
                //   label: "触发电子围栏离开提货点",
                //   disabled: (row) => !row.buttonList.includes(880),
                //   click: (row) => {
                //     this.submitLeaveCarryPoint(row)
                //   }
                // },
                /*{
                  label: "label_tms_waybill_arrive_sign_point",
                  disabled: (row) => !row.buttonList.includes(890),
                  if: () => this.waybillTaskType == "WAIT_EXECUTE_TASK",
                  click: (row) => {
                    this.submitArriveSignPoint(row)

                  }
                },*/
                { label: 'label_download_attachment', click: (row) => { this.exportAttach(row) } },
                {
                  label: 'label_report_exception',
                  click: (row) => {
                    this.getOrderByTaskId(row)
                  }
                },
                {
                  label: "预约申报",
                  click: row => this.appointmentReportFn(row)
                },
                {
                  label: "label_upload_track",
                  click: () => {
                    this.$router.push('/page/tms/track/track_import')
                  }
                },
              ]
            }
          ]
        }
      ]
    },
    clickReservationIssued() {
      if (_.isEmpty(this.selection)) {
        return this.$message.error(this.$tt('tip_please_select_order'))
      }
      // 预约需要显示时间
      this.$refs.waybillTaskIssuedRef.show(this.selection,true)
    },
    clickTaskIntercept() {
      if (_.isEmpty(this.selection)) {
        return this.$message.error(this.$tt('tip_please_select_order'))
      }
      // if (this.selection.some(it => !it.buttonList.includes(760))) {
      //   return this.$message.error(this.$tt('tip_part_order_status_error_no_operate'))
      // }
      this.$refs.waybill_task_intercept.show(this.selection)
    },
    clickTaskIssued() {
      if (_.isEmpty(this.selection)) {
        return this.$message.error(this.$tt('tip_please_select_order'))
      }
      this.$refs.waybillTaskIssuedRef.show(this.selection)
    },
    clickRecallScheduling() {
      if (_.isEmpty(this.selection)) {
        return this.$message.error(this.$tt('tip_please_select_order'))
      }
      // if (this.selection.some(it => !it.buttonList.includes(650))) {
      //   return this.$message.error(this.$tt('tip_part_order_status_error_no_operate'))
      // }
      this.$confirm(this.$tt('label_tms_waybill_confirm_cancel_scheduling'), this.$tt('label_tip'), {
        type: 'warning'
      }).then(() => {
        this.submitRecallScheduling(this.selection);
      }).catch(() => {
      });
    },
    submitTaskIssued(items) {
      this.$ajax({
        url: "/tms-api/waybill/TmsTaskInfoOperationRpc/taskIssued.json",
        method: 'post', type: 'json',
        loading: true,
        data: {
          waybillIdAndVersionList: items.map(it => {
            return {
              id: it.id,
              version: it.version,
            }
          }),
        },
        success: (res) => {
          this.$message.success(this.$tt('tip_submit_success'))
          this.reload()
        },
      })
    },
    submitRecallScheduling(items) {
      this.$ajax({
        url: "/tms-api/waybill/TmsTaskInfoOperationRpc/recallScheduling.json",
        method: 'post', type: 'json',
        loading: true,
        data: {
          waybillIdAndVersionList: items.map(it => {
            return {
              id: it.id,
              version: it.version,
            }
          }),
        },
        success: (res) => {
          this.$message.success(this.$tt('tip_submit_success'))
          this.reload()
        },
      })
    },
    clickRecallLssued() {
      if (_.isEmpty(this.selection)) {
        return this.$message.error(this.$tt('tip_please_select_order'))
      }
      // if (this.selection.some(it => !it.buttonList.includes(780))) {
      //   return this.$message.error(this.$tt('tip_part_order_status_error_no_operate'))
      // }
      this.$confirm(this.$tt('label_tms_waybill_confirm_cancel_delivery'), this.$tt('label_tip'), {
        type: 'warning'
      }).then(() => {
        this.submitRecallLssued(this.selection);
      }).catch(() => {
      });
    },
    submitRecallLssued(items) {
      this.$ajax({
        url: "/tms-api/waybill/TmsTaskInfoOperationRpc/recallLssued.json",
        method: 'post', type: 'json',
        loading: true,
        data: {
          waybillIdAndVersionList: items.map(it => {
            return {
              id: it.id,
              version: it.version,
            }
          }),
        },
        success: (res) => {
          this.$message.success(this.$tt('tip_submit_success'))
          this.reload()
        },
      })
    },

    submitTaskReceive(row) {
      this.$confirm(this.$tt('label_tms_waybill_confirm_take_orders'), this.$tt('label_tip'), {
        type: 'warning'
      }).then(() => {
        this.$ajax({
          url: "/tms-api/waybill/TmsTaskInfoOperationRpc/taskReceive.json",
          method: 'post', type: 'json',
          loading: true,
          data: {
            waybillIdAndVersionList: [{
              id: row.id,
              version: row.version,
            }],
          },
          success: (res) => {
            this.$message.success(this.$tt('tip_submit_success'))
            this.reload()
          },
        })
      }).catch(() => {
      });

    },

    submitDispatch(row) {
      this.$confirm(this.$tt('label_tms_waybill_confirm_depart'), this.$tt('label_tip'), {
        type: 'warning'
      }).then(() => {
        this.$ajax({
          url: "/tms-api/waybill/TmsTaskInfoOperationRpc/dispatch.json",
          method: 'post', type: 'json',
          loading: true,
          data: {
            waybillIdAndVersionList: [{
              id: row.id,
              version: row.version,
            }],
          },
          success: (res) => {
            this.$message.success(this.$tt('tip_submit_success'))
            this.reload()
          },
        })
      }).catch(() => {
      });
    },

    submitArrivePoint(row) {
      this.$confirm(this.$tt('label_tms_waybill_confirm_arrive_pick_point'), this.$tt('label_tip'), {
        type: 'warning'
      }).then(() => {
        this.$ajax({
          url: "/tms-api/waybill/TmsTaskInfoOperationRpc/arrivePoint.json",
          method: 'post', type: 'json',
          loading: true,
          data: {
            waybillIdAndVersionList: [{
              id: row.id,
              version: row.version,
            }],
          },
          success: (res) => {
            this.$message.success(this.$tt('tip_submit_success'))
            this.reload()
          },
        })
      }).catch(() => {
      });
    },

    submitLeaveCarryPoint(row) {
      this.$confirm(this.$tt('label_tms_waybill_confirm_leave_pick_point'), this.$tt('label_tip'), {
        type: 'warning'
      }).then(() => {
        this.$ajax({
          url: "/tms-api/waybill/TmsTaskInfoOperationRpc/leaveCarryPoint.json",
          method: 'post', type: 'json',
          loading: true,
          data: {
            id: row.id,
            version: row.version,
          },
          success: (res) => {
            this.$message.success(this.$tt('tip_submit_success'))
            this.reload()
          },
        })
      }).catch(() => {
      });
    },

    submitArriveSignPoint(row) {
      this.$confirm(this.$tt('label_tms_waybill_confirm_arrive_sign_point'), this.$tt('label_tip'), {
        type: 'warning'
      }).then(() => {
        this.$ajax({
          url: "/tms-api/waybill/TmsTaskInfoOperationRpc/arriveSignPoint.json",
          method: 'post', type: 'json',
          loading: true,
          data: {
            id: row.id,
            version: row.version,
          },
          success: (res) => {
            this.$message.success(this.$tt('tip_submit_success'))
            this.reload()
          },
        })
      }).catch(() => {
      });
    },
    exportExcel() {
      this.$downloadFile({
        loading: true,
        type: 'json',
        url: '/tms-api/waybill/TmsTaskInfoQueryRpc/exportExcel.json',
        data: {
          taskIds: this.selection.map(it => it.id),
          waybillTaskType: this.waybillTaskType,
          ...this.getSuperTablePage().getSearchFormData()
        },
      })
    },
    exportAttach(row) {
      this.$downloadFile({
        loading: true,
        method: 'get',
        data: {
          taskId: row.id,
        },
        url: `/tms-api/waybill/TmsTaskInfoQueryRpc/zipDownload.json?taskId=${row.id}`,
      })
    },
    getOrderByTaskId(row) {
      this.orderNoData = []
      this.$ajax({
        url: "/tms-api/task/TmsBaseAnomalyRpc/getOrderByTaskId.json",
        data: {
          taskId:row.id
        },
        success: (res) => {
          this.orderNoData = res.data.rows
          this['$refs']['report_anomaly'].showNew({
            data: {
              orderType: 'TRANSPORT',
              waybillTaskCode:row.waybillTaskCode
            },
            onlySetData: true,
          })
        },
      })

    },


    appointmentReportFn(row) {
      let rows = row ? [row] : this.selection
      if (rows.length === 0) {
        this.$message.error('请先选择需要处理的数据')
        return
      }

      this['$refs']['appointmentReportRer'].show(rows)

    }
  }
};

</script>
<style></style>
